
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Net;
using MonoTouch.AudioToolbox;
using MonoTouch.CoreFoundation;
using MonoTouch.AVFoundation;
using System.Drawing;
using MonoTouch.ObjCRuntime;
using System.IO;
using System.Diagnostics;

namespace Hanselminutes
{
	public class Application
	{
		static void Main (string[] args)
		{
			UIApplication.Main (args);
		}
	}

	public partial class AppDelegate : UIApplicationDelegate
	{
		UIImageView _splashView;
		UITabBarController _tabs;
		bool _backgroundSupported;
		
		public override bool FinishedLaunching (UIApplication app, NSDictionary options)
		{
			
			var device = UIDevice.CurrentDevice;
			if(device.RespondsToSelector(new Selector("isMultitaskingSupported")))
			{
				_backgroundSupported = UIDevice.CurrentDevice.IsMultitaskingSupported;	
			}
			
			Debug.WriteLine("Background supported? " + _backgroundSupported);
			
			var hanselRepo = new Hanselminutes();
			var hansel = new HomeViewController(hanselRepo) {
				Title = "Hanselminutes"
			};
			var hNav = new UINavigationController(hansel) {
				Title = hansel.Title
			};
			hNav.NavigationBar.BarStyle = UIBarStyle.Black;
			hNav.NavigationBar.TintColor = UIColor.FromRGB(177/255.0f, 187/255.0f, 103/255.0f);
			
			var h9Repo = new HanselminutesOn9();
			var hansel9 = new HomeViewController(h9Repo) {
				Title = "Hanselminutes on 9"
			};
			var h9Nav = new UINavigationController(hansel9) {
				Title = hansel9.Title
			};
			h9Nav.NavigationBar.BarStyle = UIBarStyle.Black;
			h9Nav.NavigationBar.TintColor = UIColor.FromRGB(235/255.0f, 82/255.0f, 22/255.0f);
			
			_tabs = new UITabBarController();
			_tabs.SetViewControllers(new UIViewController[] { hNav, h9Nav }, false);
			
			window.AddSubview(_tabs.View);
			
			window.MakeKeyAndVisible ();
			
			showSplashScreen();
			return true;
		}

		void showSplashScreen ()
		{
			_splashView = new UIImageView(new RectangleF(0f, 0f, 320f, 480f));
			_splashView.Image = UIImage.FromFile("Default.png");
			window.AddSubview(_splashView);
			window.BringSubviewToFront(_splashView);
			UIView.BeginAnimations("SplashScreen");
			UIView.SetAnimationDuration(0.5f);
			UIView.SetAnimationDelegate(this);
			UIView.SetAnimationTransition(UIViewAnimationTransition.None, window, true);
			UIView.SetAnimationDidStopSelector(new Selector("StartupAnimationDone"));
		    _splashView.Alpha = 0f;
		    _splashView.Frame = new RectangleF(-60f, -60f, 440f, 600f);
		    UIView.CommitAnimations();
		}

		public void StartupAnimationDone()
		{
			_splashView.RemoveFromSuperview();
			_splashView.Dispose();
		}
		
		public override void DidEnterBackground (UIApplication application)
		{
			Debug.WriteLine("Entered background...");
		}
		
		public override void WillEnterForeground (UIApplication application)
		{
			Debug.WriteLine("Entered foreground...");
		}
		
		public override void ReceivedRemoteNotification (UIApplication application, NSDictionary userInfo)
		{
			// TODO: Implement - see: http://go-mono.com/docs/index.aspx?link=T%3aMonoTouch.Foundation.ModelAttribute
		}
		
		
		
		public override void WillTerminate (UIApplication application)
		{
			// Clean up Partial files...
			var documents = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
			var document = Directory.GetFiles(documents);
			foreach(var file in document.Where(x => x.Contains("_partial.mp3")))
			{	
				Console.WriteLine ("Should delete - " + file);
				File.Delete(file);
			}
			//var partialFilePath = Path.Combine(documents, fileName + "_partial.mp3");
		}

		
		// This method is required in iPhoneOS 3.0
		public override void OnActivated (UIApplication application)
		{
		}
	}
}
